#ifndef _drv_pwmMotors_H
#define _drv_pwmMotors_H
#include "stdbool.h"
#include "encode_def.h"
#include "tim.h"
#include "Config.h"
#define PI		3.14159265f
#define TWOPI	6.28318531f

#define SINARRAYSIZE	4092
#define SINARRAYSCALE	32767

#define SYBOLNUM(a)  (a>=0.0?1.0:-1.0)


typedef void (*Motor_C)(void);
typedef void (*Motor_U)(uint16_t a,uint16_t b,uint16_t c);

typedef enum
{
	ZY_Model_torque=0,
	ZY_Model_Velocity=1,
	ZY_Model_Angle=2
}ZY_ControlModelType;

typedef struct
{
	ZY_ControlModelType model;
	float               value;                
}ZY_Control_t;
/********************************************************************************************

								pwm driver

********************************************************************************************/

typedef struct
{
	int        Phase[3];
	uint8_t    pwm_en_dn;
	Motor_U    set_pwm;
	Motor_C    start_pwm;
}pwm_drive_t;

#define enable_pwm		1
#define disnable_pwm	0

#define postion_maxpool	9

/********************************************************************************************

							motor rotor position

********************************************************************************************/
typedef struct
{
	uint8_t                     Pole_Paris;         //电机极对数
	uint8_t                     MaxPower;           //电机最大功率
	uint32_t                    LeadAngle;          //电机超前角
	float                       motor_power;	      //电机实际功率
	bool                        EnableStop;			    //刹车使能
	pwm_drive_t                 pwm_drive;          //电机PWM驱动
	Encode_Def_t                encoder;            //电机编码器
	float                       rotor;              //转子位置
}ZY_motor_drive_t;

typedef struct 
{
	float kp;
	float ki;
	float kd;
	int err;					            //当前误差
	int err_last;				            //上次误差
	int err_last_next;			            //上上次误差
}ZY_PID_t;
/********************************************************************************************

							motor speed

********************************************************************************************/
/**********************************************
电机速度结构体
**********************************************/
typedef struct 
{
	ZY_PID_t velocity_pid;
	int target_speed;			            //目标速度
	int current_speed;			            //当前速度
	bool enable;		                    //速度输出使能/刹车
	uint32_t postion;		               //电机转子位置
	uint32_t last_postion;	           //电子转子上次位置
	ZY_ControlModelType  control_model;           //电机控制模式
	ZY_motor_drive_t *motor_drive;
}ZY_FOC_Move_t;

void ZY_Motor_Init(ZY_motor_drive_t *motor_tcb,uint8_t pole_paris,float power,Motor_C start_pwm,Motor_U set_pwm);
void ZY_Motor_RotorPosition(ZY_motor_drive_t *motor_tcb);
void Motor_Mixer_RotorPosition(ZY_motor_drive_t *motor_tcb);
void ZY_Set_Power_Motor(ZY_motor_drive_t *motor_drive, float angle, float power);
void ZY_SetMotorPWM(ZY_motor_drive_t *motor_tcb);
void ZY_initSinArray(void);
#endif
